<html>
<head>
<title>coons.inc</title>
<style type="text/css">
<!--
pre{ font-family: "Courier New", Courier, monospace;
     background-color: #fafafa;
     border: 1px solid ; border-color: #000000;
     padding: 5pt;}

pre a:hover {background-color: #eeeeff;}

.med {color: #880088; font-weight: bold;}    /*Media*/
.func {color: #880088;}                      /*Functions (float, vector, string)*/
.mat {color: #880088; font-weight: bold;}    /*Materials*/
.squig {color: #0000dd;}                     /*{}*/
.cammod {color: #880088;}                    /*Camera modifiers*/
.glob {color: #880088; font-weight: bold;}   /*Global*/
.ident {font-weight: bold;}                  /*Identifiers*/
.obmod {color: #880088;}                     /*Object modifiers*/
.globmod {color: #880088;}                   /*Global modifiers*/
.dirc {color: #880088; font-weight: bold;}   /*Directives*/
.msg {color: #880088; font-weight: bold;}    /*Message streams*/
.atmo {color: #880088; font-weight: bold;}   /*Atmospheric objects*/
.matmod {color: #880088;}                    /*Material modifiers*/
.io {color: #880088; font-weight: bold;}     /*File I/O*/
.slcom {color: #009922; font-style: italic;} /*Single line comment*/
.obj {color: #880088; font-weight: bold;}    /*Objects*/
.mod {color: #880088;}                       /*Modifiers (general)*/
.inter {color: #880088; font-weight: bold;}  /*Interior*/
.atmod {color: #880088;}                     /*Atmospheric modifiers*/
.str {color: #ff0000;}                       /*Strings*/
.medmod {color: #880088;}                    /*Media modifiers*/
.num {color: #009999;}                       /*Numbers*/
.ptrn {color: #880088;}                      /*Patterns*/
.mlcom {color: #009922; font-style: italic;} /*Multi-line comment*/
.math {color: #ff0000;}                      /*Mathematical operators*/
.cam {color: #880088; font-weight: bold;}    /*Camera*/
.lite {color: #880088; font-weight: bold;}   /*Light source*/
.cond {color: #880088; font-weight: bold;}   /*Conditionals*/
.litmod {color: #880088;}                    /*Light modifiers*/
.dot {color: #880088;}                       /*Dot operators*/

-->
</style>
</head>
<body>
<p>coons.inc :</p>
<pre><span class="slcom">// Persistence of Vision Ray Tracer Include File</span>
<span class="slcom">// File: coons.inc</span>
<span class="slcom">// Vers: 3.5</span>
<span class="slcom">// Desc: Macro that creates a bilinear interpolated &quot;coons surface&quot;. The result</span>
<span class="slcom">//       is a mesh2 object.</span>
<span class="slcom">// Date: 2002/04/27</span>
<span class="slcom">// Auth: Ingo Janssen</span>

<span class="dirc">#version</span> <span class="num">3.5</span>;
<span class="dirc">#include</span> <span class="str">&quot;makemesh.inc&quot;</span>

<span class="mlcom">/*======
Coons(Spl1, Spl2, Spl3, Spl4, Iter_U, Iter_V, FileName): Generates a &quot;coons
           surface&quot;. The surface is defined by four splines, all attached head
           to tail to the previous / next one.
           The uv_coordinates come from the square &lt;0,0&gt; - &lt;1,1&gt;.

Spl1 - 4 : The four spline that define the surface.
           The splines are evaluated from t=0 to t=1.
Iter_U   : The resolution for the splines 1 and 3.
Iter_V   : The resolution for the splines 2 and 4.
FileName : The name of the file to whitch the mesh will be written. If is an
           empty string (&quot;&quot;), no file will be written.
           If the file extension is 'obj' a Wavefront objectfile will be written.
           If the extension is 'pcm' a compressed mesh file is written.
           If a file name is given, the macro will first check if it already exists.
           If that is so, it will try to parse the existing file unless it's a '*.obj',
           '*.pcm' or '*.arr' file as POV-Ray can not read them directly. In this case a new
           mesh will be generated, but the existing files will _not_ be over-written.
*/</span>

<span class="dirc">#macro</span> <a name="Coons"><span class="ident">Coons</span></a>(Spl1, Spl2, Spl3, Spl4, Iter_U, Iter_V, FileName)
   <span class="dirc">#declare</span> <a name="Build"><span class="ident">Build</span></a><span class="math">=</span>CheckFileName(FileName);
   <span class="cond">#if</span>(<a href="#Build">Build</a><span class="math">=</span><span class="num">0</span>)
      <span class="msg">#debug</span> <span class="func">concat</span>(<span class="str">&quot;\n Parsing mesh2 from file: &quot;</span>, FileName, <span class="str">&quot;\n&quot;</span>)
      <span class="dirc">#include</span> FileName
      <span class="obj">object</span><span class="squig">{</span>Surface<span class="squig">}</span>
   <span class="cond">#else</span>
      <span class="dirc">#local</span> NumVertices<span class="math">=</span>(Iter_U<span class="math">+</span><span class="num">1</span>)<span class="math">*</span>(Iter_V<span class="math">+</span><span class="num">1</span>);
      <span class="dirc">#local</span> NumFaces<span class="math">=</span>Iter_U<span class="math">*</span>Iter_V<span class="math">*</span><span class="num">2</span>;
      <span class="msg">#debug</span> <span class="func">concat</span>(<span class="str">&quot;\n Calculating &quot;</span>, <span class="func">str</span>(NumVertices,<span class="num">0</span>,<span class="num">0</span>), <span class="str">&quot; vertices for &quot;</span>,<span class="func">str</span>(NumFaces,<span class="num">0</span>,<span class="num">0</span>), <span class="str">&quot; triangles\n\n&quot;</span>)
      <span class="dirc">#declare</span> <a name="VecArr"><span class="ident">VecArr</span></a><span class="math">=</span><span class="func">array</span>[NumVertices]   
      <span class="dirc">#declare</span> <a name="NormArr"><span class="ident">NormArr</span></a><span class="math">=</span><span class="func">array</span>[NumVertices]   
      <span class="dirc">#local</span> UVArr<span class="math">=</span><span class="func">array</span>[NumVertices]      
      <span class="dirc">#local</span> Spl1_0<span class="math">=</span>Spl1(<span class="num">0</span>);
      <span class="dirc">#local</span> Spl2_0<span class="math">=</span>Spl2(<span class="num">0</span>);
      <span class="dirc">#local</span> Spl3_0<span class="math">=</span>Spl3(<span class="num">0</span>);
      <span class="dirc">#local</span> Spl4_0<span class="math">=</span>Spl4(<span class="num">0</span>);
      <span class="dirc">#local</span> UStep<span class="math">=</span><span class="num">1</span><span class="math">/</span>Iter_U;
      <span class="dirc">#local</span> VStep<span class="math">=</span><span class="num">1</span><span class="math">/</span>Iter_V;
      <span class="dirc">#local</span> Count<span class="math">=</span><span class="num">0</span>;
      <span class="dirc">#local</span> I<span class="math">=</span><span class="num">0</span>;
      <span class="cond">#while</span> (I<span class="math">&lt;</span><span class="math">=</span><span class="num">1</span>)
         <span class="dirc">#local</span> Im<span class="math">=</span><span class="num">1</span><span class="math">-</span>I;
         <span class="dirc">#local</span> J<span class="math">=</span><span class="num">0</span>;
         <span class="cond">#while</span> (J<span class="math">&lt;</span><span class="math">=</span><span class="num">1</span>)
            <span class="dirc">#local</span> Jm<span class="math">=</span><span class="num">1</span><span class="math">-</span>J;
            <span class="dirc">#local</span> C0<span class="math">=</span>Im<span class="math">*</span>Jm<span class="math">*</span>(Spl1_0)<span class="math">+</span>Im<span class="math">*</span>J<span class="math">*</span>(Spl2_0)<span class="math">+</span>I<span class="math">*</span>J<span class="math">*</span>(Spl3_0)<span class="math">+</span>I<span class="math">*</span>Jm<span class="math">*</span>(Spl4_0);
            <span class="dirc">#local</span> P0<span class="math">=</span>LInterpolate(I, Spl1(J), Spl3(Jm)) <span class="math">+</span> 
               LInterpolate(Jm, Spl2(I), Spl4(Im))<span class="math">-</span>C0;
            <span class="dirc">#declare</span> <a name="VecArr+"><span class="ident">VecArr</span></a>[Count]<span class="math">=</span>P0;
            <span class="dirc">#local</span> UVArr[Count]<span class="math">=</span><span class="math">&lt;</span>J,I<span class="math">&gt;</span>;
            <span class="dirc">#local</span> J<span class="math">=</span>J<span class="math">+</span>UStep;
            <span class="dirc">#local</span> Count<span class="math">=</span>Count<span class="math">+</span><span class="num">1</span>;
         <span class="cond">#end</span>
         <span class="msg">#debug</span> <span class="func">concat</span>(
            <span class="str">&quot;\r Done &quot;</span>, <span class="func">str</span>(Count,<span class="num">0</span>,<span class="num">0</span>),<span class="str">&quot; vertices :         &quot;</span>,
            <span class="func">str</span>(<span class="num">100</span><span class="math">*</span>Count<span class="math">/</span>NumVertices,<span class="num">0</span>,<span class="num">2</span>),<span class="str">&quot; %&quot;</span>
         )
         <span class="dirc">#local</span> I<span class="math">=</span>I<span class="math">+</span>VStep;
      <span class="cond">#end</span>
      <span class="msg">#debug</span> <span class="str">&quot;\r Normals                                  &quot;</span>
      <span class="dirc">#local</span> Count<span class="math">=</span><span class="num">0</span>;
      <span class="dirc">#local</span> I<span class="math">=</span><span class="num">0</span>;
      <span class="cond">#while</span> (I<span class="math">&lt;</span><span class="math">=</span>Iter_V)
         <span class="dirc">#local</span> J<span class="math">=</span><span class="num">0</span>;
         <span class="cond">#while</span> (J<span class="math">&lt;</span><span class="math">=</span>Iter_U)
            <span class="dirc">#local</span> Ind<span class="math">=</span>(I<span class="math">*</span>Iter_U)<span class="math">+</span>I<span class="math">+</span>J;
            <span class="dirc">#local</span> P0<span class="math">=</span><a href="#VecArr+">VecArr</a>[Ind];
            <span class="cond">#if</span>(J<span class="math">=</span><span class="num">0</span>)
               <span class="dirc">#local</span> P1<span class="math">=</span>P0<span class="math">+</span>(P0<span class="math">-</span><a href="#VecArr+">VecArr</a>[Ind<span class="math">+</span><span class="num">1</span>]);
            <span class="cond">#else</span>
               <span class="dirc">#local</span> P1<span class="math">=</span><a href="#VecArr+">VecArr</a>[Ind<span class="math">-</span><span class="num">1</span>];
            <span class="cond">#end</span>
            <span class="cond">#if</span> (J<span class="math">=</span>Iter_U)
               <span class="dirc">#local</span> P2<span class="math">=</span>P0<span class="math">+</span>(P0<span class="math">-</span><a href="#VecArr+">VecArr</a>[Ind<span class="math">-</span><span class="num">1</span>]);
            <span class="cond">#else</span>
               <span class="dirc">#local</span> P2<span class="math">=</span><a href="#VecArr+">VecArr</a>[Ind<span class="math">+</span><span class="num">1</span>];
            <span class="cond">#end</span>
            <span class="cond">#if</span> (I<span class="math">=</span><span class="num">0</span>)
               <span class="dirc">#local</span> P3<span class="math">=</span>P0<span class="math">+</span>(P0<span class="math">-</span><a href="#VecArr+">VecArr</a>[Ind<span class="math">+</span>Iter_U<span class="math">+</span><span class="num">1</span>]);
            <span class="cond">#else</span>
               <span class="dirc">#local</span> P3<span class="math">=</span><a href="#VecArr+">VecArr</a>[Ind<span class="math">-</span>Iter_U<span class="math">-</span><span class="num">1</span>];                       
            <span class="cond">#end</span>
            <span class="cond">#if</span> (I<span class="math">=</span>Iter_V)
               <span class="dirc">#local</span> P4<span class="math">=</span>P0<span class="math">+</span>(P0<span class="math">-</span><a href="#VecArr+">VecArr</a>[Ind<span class="math">-</span>Iter_U<span class="math">-</span><span class="num">1</span>]);
            <span class="cond">#else</span>
               <span class="dirc">#local</span> P4<span class="math">=</span><a href="#VecArr+">VecArr</a>[Ind<span class="math">+</span>Iter_U<span class="math">+</span><span class="num">1</span>];
            <span class="cond">#end</span>
            <span class="dirc">#local</span> B1<span class="math">=</span>P4<span class="math">-</span>P0;
            <span class="dirc">#local</span> B2<span class="math">=</span>P2<span class="math">-</span>P0;
            <span class="dirc">#local</span> B3<span class="math">=</span>P3<span class="math">-</span>P0;
            <span class="dirc">#local</span> B4<span class="math">=</span>P1<span class="math">-</span>P0;
            <span class="dirc">#local</span> N1<span class="math">=</span><span class="func">vcross</span>(B1,B2);
            <span class="dirc">#local</span> N2<span class="math">=</span><span class="func">vcross</span>(B2,B3);
            <span class="dirc">#local</span> N3<span class="math">=</span><span class="func">vcross</span>(B3,B4);
            <span class="dirc">#local</span> N4<span class="math">=</span><span class="func">vcross</span>(B4,B1);
            <span class="dirc">#local</span> Norm<span class="math">=</span><span class="func">vnormalize</span>((N1<span class="math">+</span>N2<span class="math">+</span>N3<span class="math">+</span>N4));      
            <span class="dirc">#declare</span> <a name="NormArr+"><span class="ident">NormArr</span></a>[Count]<span class="math">=</span>Norm;
            <span class="dirc">#local</span> J<span class="math">=</span>J<span class="math">+</span><span class="num">1</span>;
            <span class="dirc">#local</span> Count<span class="math">=</span>Count<span class="math">+</span><span class="num">1</span>;
         <span class="cond">#end</span>
         <span class="msg">#debug</span> <span class="func">concat</span>(<span class="str">&quot;\r Done &quot;</span>, <span class="func">str</span>(Count,<span class="num">0</span>,<span class="num">0</span>),<span class="str">&quot; normals : &quot;</span>,<span class="func">str</span>(<span class="num">100</span><span class="math">*</span>Count<span class="math">/</span>NumVertices,<span class="num">0</span>,<span class="num">2</span>), <span class="str">&quot; %&quot;</span>)
         <span class="dirc">#local</span> I<span class="math">=</span>I<span class="math">+</span><span class="num">1</span>;
      <span class="cond">#end</span>
      BuildWriteMesh2(<a href="#VecArr+">VecArr</a>, <a href="#NormArr+">NormArr</a>, UVArr, Iter_U, Iter_V, FileName)
   <span class="cond">#end</span>
<span class="cond">#end</span>
</pre>
</body>
</html>